<?php
/**
 * iweibo2.0
 * 
 * iphone版本登录控制器
 *
 * @author richardjia <jiawangxi@qq.com>
 * @link http://open.t.qq.com/iweibo
 * @copyright Copyright © 1998-2011. All Rights Reserved 
 * @license http://open.t.qq.com/iweibo/license
 * @version $Id: Controller_Mobile_Login.php 2011年7月18日14:48:32 richardjia $
 * @package Controller
 * @since 2.0
 */
class Controller_Mobile_Login extends Core_Controller_Action
{
     private $userModel;
     protected $savedData = array();
    
    public function preDispatch() {
        parent::preDispatch();
        $this->userModel = new Model_User_Member();
    }    


    /**
     * 获取大家都在说
     * 
     * @param int $num
     */
    private function getMsgList($num)
    {
        $apiClient = Core_Open_Api::getNoneTokenClient();
        
        $p = array('p' => 0, 'n' => $num);
        $openmsg = $apiClient->getPublic($p);
        
        $msglist = $openmsg['data']['info'];
        $msglist && Core_Lib_Base::formatTArr($msglist);
        
        $this->save('msglist', $msglist);
    }

    /**
     * 显示登录页
     */
    public function indexAction()
    {
   
    }

    /**
     * 登出
     */
    public function logoutAction()
    {
        $this->userModel->onLogout();
        $this->showMessage(Core_Comm_Modret::RET_USER_LOGOUTSUCCEED);
        exit();    
    }



    /**
     * 本地登录
     */
    public function lAction()
    {
        $username = $this->getParam('username');
        $pwd = $this->getParam('pwd');
        $autologin = $this->getParam('autologin');          
        $user = $this->userModel->onLogin($username, $pwd);
      
      
        //用户不存在
        if(empty($user))
        {
            $this->showMessage(Core_Comm_Modret::RET_USER_LOGINFAILED);
        }
 
        //取得当前accessToken
        $tokenArr = $this->userModel->onGetCurrentAccessToken();
        //如果没绑定
        if(empty($user["name"]))
        {//报没有绑定错误
           
        }
        else
        {          
            $oauthToken = $user["oauthtoken"];
            $oauthTokenSecret = $user["oauthtokensecret"];
            $name = $user['name'];
        }
        //用户登陆成功，设置会话保持
        $this->userModel->onSetCurrentUser($user['uid'], $user['nickname']);
        $this->userModel->onSetCurrentAccessToken($oauthToken, $oauthTokenSecret, $name);
        //没绑定到绑定腾讯微博页
        if(empty($name))
        {
            if(! empty($tokenArr['name']))
            {  
                $this->showMessage(Core_Comm_Modret::RET_USER_UNBOUND);          
                 return;
            }
        }
       
        //如果本地用户昵称为空则将平台用户信息赋给本地
    
        if(empty($user['nickname']))
        {
            //从平台取得用户信息
            $uInfo = Core_Open_Api::getClient()->getUserInfo();
            $userInfo['uid'] = $user['uid'];
            $userInfo['nickname'] = $uInfo['data']['nick'];
            $userInfo['gender'] = $uInfo['data']['sex'];
            $userInfo['birthyear'] = $uInfo['data']['birth_year'];
            $userInfo['birthmonth'] = $uInfo['data']['birth_month'];
            $userInfo['birthday'] = $uInfo['data']['birth_day'];
            $userInfo['nation'] = $uInfo['data']['country_code'];
            $userInfo['province'] = $uInfo['data']['province_code'];
            $userInfo['city'] = $uInfo['data']['city_code'];
            $userInfo['summary'] = $uInfo['data']['introduction'];
            $userInfo['fansnum'] = Model_User_FriendLocal::getFollowerCountByName($name, true);
            $userInfo['idolnum'] = Model_User_FriendLocal::getFolloweeCountByName($name, true);
            $userInfo['nickname'] = empty($userInfo['nickname']) ? $user['username'] :
                                                                   $userInfo['nickname'];
            $this->userModel->editUserInfo($userInfo);
            $this->userModel->onSetCurrentUser($user['uid'], $userInfo['nickname']);
        }else
        {
            $userInfo = $user;
        }
        //如果用户选中自动登录 保存令牌
        $tokenModel = new Model_Base_Token();
        $sign = Core_Comm_Token::_generate_key();
        if($autologin==1 && $tokenModel->addToken(array('uid' => $user['uid'], 'sign' => $sign)))
        {
            $authcode = Core_Comm_Token::authcode($user['uid'] . "\t" . $sign, 'ENCODE');
            $this->setCookie('iwb_token', $authcode, 3600 * 24 * 30);
        }
        
        $this->userModel->onSetLastVisit($user['uid']);
        $this->showMessage(Core_Comm_Modret::RET_SUCC);       
    }

    public function checkAction()
    {
        
    }
    
 
    public function showMessage($retCode)
    {    
        $this->save('ret', $retCode);
        $msg = Core_Comm_Modret::getMsg($retCode);
        $this->save('msg', $msg);            
        $this->output();           
    }
    
    protected function  save($key, $val)
    {                  
        $this->savedData[$key] = $val;                
        
    }
        
    protected function delete($key)
    {
        unset($this->savedData[$key]);
    }
        
    protected function output()
   {
        echo json_encode($this->savedData);
        exit();  
    }    
    
    
}